<?php
require_once('configure.php');
//require_once('components/a_konfirmasi/cetak.a_konfirmasi.php');
 
?>
 <html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>HRIS Krakatau Medika :: Printed out report</title> 
</head>

<body style="font:10px verdana;" onload = "javascript:breakeveryheader()"><pre> 
<?php  
function get_jadwal($karyawan_id , $periode_id , $date){
	$day = date('d',strtotime($date));
	$query = "SELECT * FROM a_temp_jadwal_karyawan 
		WHERE karyawan_id = {$karyawan_id}  "; 
	$res = my_query($query);
	$row = my_fetch_array($res); 
	return $row['h'.$day];
}


function check_finger($karyawan_id , $date){
	$query = "SELECT * FROM temp_proses_finger 
		WHERE ( jam_in = '00:00:01' OR jam_out = '00:00:01' )
		AND karyawan_id = {$karyawan_id } AND	( DATE( proses_date ) = DATE('{$date}') ) ";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		return true;
	}
	return false;
}

function total_masuk($karyawan_id , $prefix = false){
	$query = "SELECT * FROM  temp_tmn_hari_new WHERE karyawan_id = {$karyawan_id} ";
	
	if($prefix){
		$query .= " AND status_absen = '{$prefix}' ";
	}else{
		$query .= " AND 
		( 
			status_absen = 'N' OR
			status_absen = '1' OR
			status_absen = '2' OR
			status_absen = '3' 
		)";
	}
	$query .= " GROUP BY tgl_index ";
 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return my_num_rows($result);
}

function get_saldo_menit($karyawan_id){
	$query = "SELECT   SUM(  margin_calc  ) AS a_margin FROM temp_proses_finger 
		WHERE karyawan_id = {$karyawan_id} ";
	$res = my_query($query);
	$row = my_fetch_array($res); 
	return $row['a_margin'];
}

function get_lembur($karyawan_id , $date){
	$query = "SELECT jumlah_jam , tugas, dari_jam, sampai_jam FROM  a_spl WHERE karyawan_id = {$karyawan_id} AND DATE(tanggal) = '{$date}' ";
	
	$res = my_query($query);
	$row = my_fetch_array($res); 
	return $row;
}


function get_kjk($karyawan_id){
	$query = "SELECT njam FROM temp_tmn_kjk_new WHERE karyawan_id = {$karyawan_id}";
$res = my_query($query);
	$row = my_fetch_array($res); 
	return $row['njam'];
}
function get_lembur_otomatis($karyawan_id){
$query = "SELECT nhari FROM temp_tmn_khk_new WHERE karyawan_id = {$karyawan_id}";
$res = my_query($query);
	$row = my_fetch_array($res); 
	return $row['nhari'];
}

function get_jadwal_waktu($kode , $hari_id){
	$datas = array('1','2','3','4','5','6','7','8','9');
	if($kode == 'N' ){
		$query = "SELECT jam_masuk , jam_pulang FROM a_jadwal_kerja 
			WHERE waktu_kerja_id = '1' AND hari_id = {$hari_id} AND kelompok = 'N' ";
		$res = my_query($query);
		$row = my_fetch_array($res);
		return $row;
	}elseif( in_array($kode , $datas)){
	$query = "SELECT jam_masuk , jam_pulang FROM a_jadwal_kerja 
		WHERE waktu_kerja_id = '4' AND hari_id = {$hari_id} AND kelompok = '{$kode}' ";
		$res = my_query($query);
		$row = my_fetch_array($res);
		return $row;
	}
	$row = array(
		'jam_masuk'=>'00:00:00',
		'jam_pulang'=>'00:00:00'
	);
	return $row;
}

function get_hari_absen($karyawan_id,$periode_id,$date){
	$index_date = (int) date('d',strtotime($date));
	$query = "SELECT status_absen FROM temp_tmn_hari_new WHERE karyawan_id = {$karyawan_id} AND tgl_index = {$index_date} LIMIT 1";
	 $res = my_query($query);
	$row = my_fetch_array($res);
	return $row['status_absen'];
}

function cek_status_absen($status_absen){
	$allow = array('1','2','3','4','5','6','7','8','9','N','TF');
	if(in_array( $status_absen, $allow )){
		return true;
	}
	return false;
}

function get_keterangan($karyawan_id , $date){
	$tukar_dinas = "SELECT   tukar_karyawan_id FROM a_tukar_shift WHERE karyawan_id = {$karyawan_id} AND DATE(tanggal) = '{$date}' ";
	$result_tukar_dinas = my_query($tukar_dinas );
	if( my_num_rows($result_tukar_dinas) > 0 ){
		$row_tukar_dinas = my_fetch_array($result_tukar_dinas);
		$get_karyawan_id = $row_tukar_dinas['tukar_karyawan_id'];
		$karyawan = loaddata_karyawan($get_karyawan_id);
		return 'Tukar '.$karyawan['nik'];
	}
	
	$tukar_dinas = "SELECT   karyawan_id FROM a_tukar_shift WHERE tukar_karyawan_id = {$karyawan_id}  AND DATE(tanggal) = '{$date}'  ";
	$result_tukar_dinas = my_query($tukar_dinas );
	if(  my_num_rows($result_tukar_dinas) > 0  ){
		$row_tukar_dinas = my_fetch_array($result_tukar_dinas);
		$get_karyawan_id = $row_tukar_dinas['karyawan_id'];
		$karyawan = loaddata_karyawan($get_karyawan_id);
		return 'Tukar '.$karyawan['nik'];
	}
	return '';
}
$status_hadir_available = array(
	'N','1',
	'2','3',
	'4','5',
	'6','7',
	'8','9',
);
$periode_absen_id=get_periode_absen() ;
	if($periode_id==0){
		$periode_id=$periode_absen_id;
	}	
	if( $periode_absen_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	
	$dperiode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulans = array(
		1=>'Januari',
		2=>'Februari',
		3=>'Maret',
		4=>'April',
		5=>'Mei',
		6=>'Juni',
		7=>'Juli',
		8=>'Agustus',
		9=>'September',
		10=>'Oktober',
		11=>'November',
		12=>'Desember',
	);
	$bln = (int)date('m' ,strtotime($dperiode['tanggal']));
	$namabulan = $bulans[$bln]; 	
	$thn = date('Y' ,strtotime($dperiode['tanggal']));
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id,a.nik FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 
				AND (a.nik BETWEEN '{$_GET['nik_start']}' AND '{$_GET['nik_end']}' )
				";
 
	$result = my_query($query );
	while($row = my_fetch_array($result )):
		$karyawan = loaddata_karyawan($row['karyawan_id']);	
		$jabatan = my_get_data_by_id('jabatan','jabatan_id',$karyawan['jabatan_id']);
		$golongan_jabatan = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',(int)$jabatan['golongan_jabatan_id']); 
		$pbi = my_get_data_by_id('pos_biaya','pos_biaya_id',(int)$jabatan['pos_biaya_id']);

?>
  <?php echo 'SLIP ABSENSI  ( '. $namabulan.' '.$thn.' )'; ?> 			         			                      
  NIK      : <?php echo sprintf('%07d',$karyawan['nik']);?>                      T.M.B.  : <?php echo date('d-m-Y',strtotime($karyawan['tmb'])); ?>
  
  Dept/CCC : <?php echo $jabatan['nomor_posisi']; ?> / <?php echo str_pad($pbi['code'],4);?>              St.Kary : <?php echo ucfirst(str_replace( "Karyawan ","",$karyawan['status_karyawan'])); ?>                     
  Nama     : <?php echo str_pad( $karyawan['nama_gelar'] ,29);?>St.Kel. : <?php echo $karyawan['ptkp_status'].($karyawan['ptkp_tanggung']>0? $karyawan['ptkp_tanggung']: '')?>
  
  Jabatan  : <?php 
  echo (strlen($jabatan['nama_jabatan'])<51) ? $jabatan['nama_jabatan']: substr($jabatan['nama_jabatan'],0,47).'...';
  ?>
  
  _______________________________________________________________  

<?php echo '  '; ?>Tangg   JDW  RLS  Kehadiran     Overtime      JAM  Keterangan
<?php 
$qr = "
SELECT a.proses_date,a.jam_in,a.jam_out ,b.status_absen FROM temp_proses_finger a
INNER JOIN temp_tmn_hari_new	 b ON a.karyawan_id = b.karyawan_id 
WHERE  a.karyawan_id = {$row['karyawan_id']}
GROUP BY a.proses_date
ORDER BY a.proses_date ASC";
$rs = my_query($qr);
$shofinger = 0;
$trein = 0;
$jotime = 0;
$gagal_finger =0;
$kehadiran=0;
while($ry = my_fetch_array($rs)){

$hari_id = 1 + date( 'w', strtotime($ry['proses_date']));
$jadwal = get_jadwal($row['karyawan_id'] , $periode_id , $ry['proses_date']);
$status_absen = get_hari_absen($row['karyawan_id'],$periode_id,$ry['proses_date']);
if(in_array($status_absen , $status_hadir_available )){
	$kehadiran++;
	if(check_finger($row['karyawan_id'] , $ry['proses_date'])){
		$gagal_finger++;
	}
}
$jtime = get_jadwal_waktu($status_absen , $hari_id);
$keterangan = get_keterangan($row['karyawan_id'] , $ry['proses_date']);
$otime =  get_lembur($row['karyawan_id'] , $ry['proses_date']);
$ot = (int) $otime['jumlah_jam'];
$jotime +=$ot;
$ry['jam_in']  =   cek_status_absen($status_absen)   ? $ry['jam_in']  : '00:00:00'   ;
$ry['jam_out'] =   cek_status_absen($status_absen)   ? $ry['jam_out'] : '00:00:00'   ;
if(cek_status_absen($status_absen)){
	$shofinger++;
	if(date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_in'])) <> '00:00'){
		$trein++;
	}
	if(date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_out'])) <> '00:00'){
		$trein++;
	}
}							echo '  ';
							echo str_pad(date('d/m' ,strtotime($ry['proses_date'])),8); //Tanggal 
					        echo str_pad($jadwal,5); //Jadwal
					        echo str_pad($status_absen,5); //Realisasi 
					        echo str_pad(	date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_in'])).' - '.
											date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_out'])),14); //Kehadiran
					        echo str_pad(	date('H:i',strtotime($ry['proses_date'].' '.$otime['dari_jam'])).' - '.
											date('H:i',strtotime($ry['proses_date'].' '.$otime['sampai_jam'])),14);  //Waktu
					        echo str_pad($ot,4); //Jadwal
					        echo substr(( $ot> 0 ? $otime['tugas'] : $keterangan) ,0, 35); //Keterangan
						 echo "\n" ; 
}
$status_finger = 2 * $shofinger;
$tread_finger = $trein;
$saldo_total = get_saldo_menit($row['karyawan_id']);
$jumlah_cuti = total_masuk(  $row['karyawan_id'] ,'C' );
$jumlah_ijin = total_masuk(  $row['karyawan_id'] ,'PP' );
$jumlah_sakit = total_masuk(  $row['karyawan_id'] ,'SD' );
$perjalanan_dinas = total_masuk(  $row['karyawan_id'] ,'BT' );
if($saldo_total <= 0 ){ 
	$saldo_jam = ceil( $saldo_total  /60 );
}else{
	$saldo_jam = floor( $saldo_total  /60 );
}
$saldo_menit =  ($saldo_total % 60 );
$saldo_jam_text = $saldo_jam < 1 ? sprintf('%03d',$saldo_jam) : sprintf('%02d',($saldo_jam -1)) ; 
$saldo_menit_text = $saldo_menit < 1 ? sprintf('%03d',$saldo_menit) : sprintf('%02d',$saldo_menit) ; 
$kjk = get_kjk($row['karyawan_id']);
$lembur_otomatis = get_lembur_otomatis($row['karyawan_id']);
						 ?>   
  Resume :
  Persentase finger <?php echo round(100 * $tread_finger / $status_finger) ; ?>% 
  Jumlah jam lembur <?php echo $jotime; ?> jam 
<?php if($saldo_jam <= 0){ 
?>
  Potongan waktu kerja (absen) <?php echo $saldo_jam_text .' jam dan '. sprintf('%02d',$saldo_menit); ?> menit
<?php } ?>
  Jumlah kehadiran <?php echo ($kehadiran - $gagal_finger); ?> hari
  Cuti <?php echo $jumlah_cuti; ?>  hari / Ijin <?php echo $jumlah_ijin;?> hari / Sakit (Surat Dokter) <?php echo $jumlah_sakit;?> hari
  Perjalan dinas <?php echo $perjalanan_dinas; ?>  hari  
  <?php if((int)$kjk > 0) echo 'KJK '. $kjk.' jam'; ?>  
  <?php if((int)$lembur_otomatis > 0)echo 'Lembur otomatis '.$lembur_otomatis.' jam'; ?>

  NB: Apabila data diatas ada yang tidak sesuai,
      dapat menghubungi/ datang langsung ke SDM
      untuk dikoreksi paling lambat tanggal 19  <?php echo $namabulan; ?> <?php echo $thn; ?>
	  
	  
  HRIS Krakatau Medika (c) 2011 - <?php echo date('Y'); ?>  
<?php echo '<P></P>';   
endwhile;?>

</pre>	
<script>
function breakeveryheader(){
if (!document.getElementById){
	alert("You need IE5 or NS6 to run this example")
	return
}
var thestyle=  "always" ;
for (i=0; i<document.getElementsByTagName("P").length; i++)
	document.getElementsByTagName("P")[i].style.pageBreakBefore=thestyle
}

</script>   
</body> 
</html>